﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.SessionState;
using AppLab.Core;

namespace AppLab.Web
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            AppDomain.CurrentDomain.SetData(".appHash", String.Empty);

            Log.Listeners.Add(new TextWriterLogListener(CreateLogger()));
            Log.WriteInfo("系统启动, 物理路径={0}",
                this.Server.MapPath(null));


            GlobalConfiguration.DataService = WebConfigurationManager.ConnectionStrings["DataService"].ConnectionString;
            GlobalConfiguration.ApplicationService = WebConfigurationManager.ConnectionStrings["ApplicationService"].ConnectionString;
            ///Log.WriteInfo("手机数据库：{0}", GlobalConfiguration.DataService);
            Log.WriteInfo("工作流数据库：{0}", GlobalConfiguration.ApplicationService);
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            //
            //Log.WriteInfo("来自{0}的用户{1}使用{2}浏览器请求{3}",
            //   this.Request.UserHostAddress,
            //   this.Request.LogonUserIdentity.Name,
            //   this.Request.Browser.Type,
            //   this.Request.RawUrl);
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Log.WriteException(this.Server.GetLastError(), "Detail is as below");
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            Log.WriteInfo("系统关闭");
        }

        private string CreateLogger()
        {
            string logDirFormat = null;
            string logPathFormat = null;
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                logDirFormat = @"{0}\Wicresoft\AppLab\Logs";
                logPathFormat = @"{0}\Wicresoft\AppLab\Logs\{1}{2}.log";
            }
            else if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                logDirFormat = "{0}/wicresoft/applab/logs";
                logPathFormat = "{0}/wicresoft/applab/logs/{1}{2}.log";
            }

            var dir = String.Format(logDirFormat,
                Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            var log = String.Format(logPathFormat,
                Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                DateTime.Today.ToString("yyyy-MM-dd"),
                AppDomain.CurrentDomain.GetData(".appHash").ToString());

            // for windows, it should return something like
            //  C:\ProgramData\Wicresoft\AppLab\Logs\xxx.log
            // while for linux, it should return something like
            //  /usr/share/wicresoft/applab/logs/xxx.log
            return log;
        }
    }
}